package com.gxa.service.impl;

import com.gxa.annotation.LoginLog;
import com.gxa.dto.ResultDto;
import com.gxa.mapper.UserMapper;
import com.gxa.pojo.User;
import com.gxa.service.LoginService;
import com.gxa.util.Response;
import com.gxa.validator.MyValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

@Service("loginByPhone")
public class LoginServiceByPhoneImpl implements LoginService {
    @Autowired
    private MyValidator validator;

    @Autowired
    private UserMapper userMapper;

    /**
     * 电话登录
     * @param user
     * @param session
     * @return
     */
    @Override
    public ResultDto login(User user, HttpSession session) {
        //数据校验
        validator.validate(user);
        User dbUser = userMapper.findByPhone(user);
        if(dbUser == null){
            Response.success(1003,"该手机号不存在");
        }
        session.setAttribute("userName",dbUser.getUserName());
        session.removeAttribute("phone");
        session.removeAttribute("code");
        user.setLastLoginTime(new Timestamp(System.currentTimeMillis()));

        userMapper.updateByUserPhone(user);
        return Response.success(200,"登录成功");
    }
}
